<?
    require_once("mvc/action/Action.php");
    require_once("cms/newsletter/om/NewsletterPeer.php");

    class EditAction extends Action {

        private $log;

        public function __construct() {
            $this->log =& LoggerManager::getLogger("EditAction");
        }

        public function execute(ActionConfig &$mapping, &$form, Request &$request, Response &$response) {

            if($form->getNewsletter()->getNewsletterId() > 0) {
                $newsletter = NewsletterPeer::retrieveByPk($form->getNewsletter()->getNewsletterId());
                if(!is_null($newsletter)) {

                    $form->setNewsletter($newsletter);

                } else {
                    $this->log->error("No newsletter found with id {$form->getNewsletter()->getNewsletterId()}.");
                    $form->getNewsletter()->setNewsletterId("");
                    $form->getNewsletter()->setBody($this->getTemplate());
                }
            } else {

                $template = str_replace(
                    array("{base}"),
                    array($request->getContext()->getInitParameter(_Newsletter::BASE)),
                    ($this->getTemplate($request))
                );

                $form->getNewsletter()->setBody($template);
            }

            return $mapping->findForward("success");

        }

        private function getTemplate(Request $request) {

ob_start();
include($request->getContext()->getInitParameter(_Newsletter::BODY_NEWSLETTER));
$template = ob_get_contents();
ob_end_clean();
return htmlentities($template);

        }

    }
?>